<template>
  <section>
    <!--高级查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="部门名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="listLoadData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>



    <!--列表-->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="id" label="部门编号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="部门描述" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="dirPath" label="路径" width="100" sortable>
      </el-table-column>
      <el-table-column prop="employee.username" label="负责人" min-width="180" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="上级部门" min-width="180" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-col :span="24" class="toolbar">
<!--      绝对相等===三个等于符号-->
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes,prev, pager, next"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size=pageSize
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>
    <!--新增弹框
    :rules="addFormRules"表单校验，-->
    <el-dialog :title=title :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="editForm">
        <el-form-item label="公司编号" prop="sn">
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司名字" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级部门" >
          <!--          <el-input v-model="addForm.parent_id" auto-complete="off"></el-input>-->
<!--          级联下拉框-->
          <el-cascader
              :options="deptTrees"
              :props="{
                checkStrictly: true,
                label:'name',
                value:'id'
              }"
              clearable></el-cascader>
        </el-form-item>
        <el-form-item label="负责人" >
<!--          <el-input v-model="addForm.parent_id" auto-complete="off"></el-input>-->
          <el-select  v-model="addForm.employee" placeholder="请选择" value-key="id">
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      deptTrees:[],
      employees:[],
      //开启高级查询
      filters: {
        name: ''
      },
      listData:[],//列表展示数据
      listLoading:true,
      sels: [],
      total:0,
      pageSize:5,
      page:1,
      //新增界面数据
      addForm: {
        sn:"",
        name:"",
        state:0,
        manager_id:null,
        parent_id:null,
        employee:{

        },
        department:{

        }
      },
      //新增弹框的开启关闭属性
      addFormVisible:false,
      title:'',
      //定义校验规则
      addFormRules: {
        //具体的规则1
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        //具体的规则2
        sn: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ]
      },
      addLoading:false
  }
  },
  methods: {
    loadTrees(){
      this.$http.get("http://localhost:8080/dept/deptTree").then(res=>{
        console.debug(res)
        this.deptTrees=res.data
      })
    },
    loadEmps(){
      this.$http.get("http://localhost:8080/emp").then(res=>{
          console.debug(res)
          this.employees=res.data
      })
    },
    //提交按钮的方法
    addSubmit(){
      this.$refs.editForm.validate((valid) => {//校验表单，refs
        //如果通过执行
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //选择的上级部门数组
            let arr=this.addForm.department;
            //重新组装parent
            this.addForm.department={
                id:arr[arr.length-1]
            }
            this.$http.put("http://localhost:8080/dept",this.addForm).then(res=>{
              console.debug(this.addForm)
              console.debug(res)
              this.addLoading=false
              let {success,message}=res.data
              if(success){
                this.$message({
                  message: '新增成功',
                  type: 'success'
                });
              }else {
                this.$message.error("网络异常 ")
              }
              //关闭模态框
              this.addFormVisible = false;
              //刷新列表
              this.listLoadData();
            })

          });
        }
      });
    },
    //新增按钮的方法
    handleAdd(){
      this.addFormVisible = true;
      this.title="新增"
      this.addForm = {
        sn:"",
        name:"",
        state:"",
        manager_id:"",
        parent_id:"",
        employee: {}
      };
    },
    batchRemove(){
      // var ids = this.sels.map(item => item.id).toString();
      //获取选中的对象的id
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        this.$http.patch('http://localhost:8080/dept',ids).then(res=>{
          let {success,message}=res.data
          if(success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error("网络异常 ")
          }
          this.listLoadData();
        })
      }).catch(() => {
        this.$message("取消删除")
      });
    },
    handleSizeChange(val) {
      this.pageSize=val;
      this.listLoadData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.listLoadData();
    },
    listLoadData(){
      let param={

        currentPage:this.page,
        pageSize:this.pageSize,
        keyword:this.filters.name
      };
      //发送post请求
      this.$http.post("http://localhost:8080/dept",param).then(res=>{
        console.debug(res.data),
        this.listData=res.data.data,
        this.total=res.data.totals,
        this.listLoading = false;
      })
    }
    ,
    selsChange: function (sels) {
      this.sels = sels;
    },
    formatSex(row, column) {
      return row.state == 0 ? '启用' : row.state == -1 ? '禁用' : '未知';
    },
    //显示删除界面
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        console.debug(row.id)
        this.$http.delete("http://localhost:8080/dept/"+row.id).then(res=>{
            console.debug(res)
          let {success,message}=res.data
          if(success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error("网络异常 ")
          }
          this.listLoadData();
        })
      }).catch(() => {
          this.$message("取消删除")
      });
    },
    //显示新增界面
    handleEdit: function (index, row) {
      //  数据回显
      this.addForm = Object.assign({}, row);
      //打开模态框
      this.addFormVisible = true;
      // 给标题换名字
      this.title="编辑";
    },

  },
  mounted() {
      this.listLoadData(),
      this.loadEmps(),
      this.loadTrees()

  }
}

</script>
<style scoped>

</style>